package com.fingard.dsp.bank.directbank.swiftpass01;

import java.util.HashMap;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.ICloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.CloseOrderState;
import com.fingard.dsp.bank.reqrespobj.closeorder.ReqCloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.RetAtsCloseOrder;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

public class SwiftPass01CloseOrder extends SwiftPass01Base implements ICloseOrder {
	public RetAtsCloseOrder closeOrder(ReqCloseOrder p_req){
		RetAtsCloseOrder retAts = new RetAtsCloseOrder(p_req.reqHeader);
		
		try{
			retAts.rdSeq = p_req.rdSeq;
			
			ActSetItem actItem = this.getBankConfig().getActSetItemByAny(p_req.ownAct, p_req.merchantId, p_req.appId, "unifiedorder");
			if(actItem == null){
				retAts.respCode = "-1";
				retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息";
				return retAts;
			}
			
			HashMap<String,String> mapParams = new HashMap<String,String>();
			
			mapParams.put("service", "unified.trade.close");
			
			/*
			 * 微信扫码付和支付宝扫码付通过指令代码区分
			 * 		SPNATIVE01:微信扫码付
			 * 		SPNATIVE02:支付宝扫码付
			 */
			if (p_req.reqHeader.commandCode.equals("SPNATIVE01")) {
				mapParams.put("version", "1.0");
			}else {
				mapParams.put("version", "2.0");
			}
			
			mapParams.put("charset", getCharset());
			mapParams.put("sign_type", "MD5");
			
			String mch_id = p_req.merchantId;
			if(StringHelper.isNullOrEmpty(mch_id)){
				mch_id = actItem.merchantId;
			}
			mapParams.put("mch_id", mch_id);
			
			mapParams.put("out_trade_no", p_req.rdSeq);
			
			String nonce_str = get_nonce_str();
			mapParams.put("nonce_str", nonce_str);
			
			String signMsg = signMessage(mapParams, actItem.saltKey);
			mapParams.put("sign", signMsg);

			String tmpXml = buildXml(mapParams);
			
			String[] tmpStrRet = sendToBank(tmpXml, "closeorder");
			
			if(tmpStrRet[0].length() == 0){
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				
				if(tmpBkReader != null){
					String status = tmpBkReader.getFirstText("/xml/status");
					retAts.respInfo = tmpBkReader.getFirstText("/xml/message");
					
					if(status!= null && status.equals("0")){
						boolean verifyResult = this.verifySign(tmpStrRet[1], actItem.saltKey, tmpSbMsg);
						
						if(verifyResult){
							retAts.errorCode = tmpBkReader.getFirstText("/xml/err_code");
							retAts.errorMsg = tmpBkReader.getFirstText("/xml/err_msg");
							
							retAts.respCode = "0";
							
							String result_code = tmpBkReader.getFirstText("/xml/result_code");
							if(result_code != null && result_code.equals("0")){
								retAts.closeState = CloseOrderState.Close_Success;
							}else if(StringHelper.hasAnyChar(result_code)){
								String errCode = tmpBkReader.getFirstText("/xml/err_code");
								if ("ACQ.TRADE_NOT_EXIST".equals(errCode) || "Order not exists".equals(errCode)) {
									retAts.closeState = CloseOrderState.Close_Success;
									retAts.errorCode = "";
									retAts.errorMsg = "";
								} else {
									retAts.closeState = CloseOrderState.Close_Fail;
								}
							} else {
								retAts.respCode = "-1";
							}
						}else{
							retAts.respCode = "-1";
							retAts.respInfo = tmpSbMsg.toString();
						}
					}else{
						retAts.respCode = "-1";
					}
				}else{
					retAts.respCode = "-1";
					retAts.respInfo = tmpSbMsg.toString();
				}
			}else if(tmpStrRet[0].equals(ConnState.FAIL)){
				retAts.respCode = "0";
				retAts.closeState = CloseOrderState.Close_Fail;
				retAts.respInfo = tmpStrRet[1];
				retAts.errorCode = tmpStrRet[0];
				retAts.errorMsg = tmpStrRet[1];
			}else{
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}
		}catch(Exception ex){
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		return retAts;
	}
}
